package io.ydjs.service.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.ydjs.api.app.index.request.IndexOperatorRequest;
import io.ydjs.api.app.index.response.IndexResponse;
import io.ydjs.common.api.PageObject;
import io.ydjs.common.utils.Constant;
import io.ydjs.common.utils.ObjectMapperUtil;
import io.ydjs.common.utils.StringUtil;
import io.ydjs.service.config.service.DictItemService;
import io.ydjs.service.index.service.IndexService;
import io.ydjs.service.user.dao.UserDao;
import io.ydjs.service.user.entity.UserEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("indexService")
public class IndexServiceImpl extends ServiceImpl<UserDao, UserEntity> implements IndexService {

    private final DictItemService dictItemService;

    public IndexServiceImpl(DictItemService dictItemService) {
        this.dictItemService = dictItemService;
    }

    @Override
    public PageObject list(IndexOperatorRequest indexOperatorRequest) {

        // 判断是筛选分组类型
        if (indexOperatorRequest.getScreeningGrouping() == 0) {
            // 自定义查询
            UserEntity userEntity = ObjectMapperUtil.convert(indexOperatorRequest, UserEntity.class);
            QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>(userEntity);
            // 数据处理
            return dataProcessing(indexOperatorRequest,queryWrapper);
        } else if (indexOperatorRequest.getScreeningGrouping() == 1){
            // 最新
            QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().orderByDesc(UserEntity::getCreateTime);
            // 数据处理
            return dataProcessing(indexOperatorRequest,queryWrapper);
        } else if (indexOperatorRequest.getScreeningGrouping() == 2){
            // 推荐
            QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .isNotNull(UserEntity::getMobile)
                    .isNotNull(UserEntity::getUserName)
                    .isNotNull(UserEntity::getAvatar)
                    .isNotNull(UserEntity::getGender)
                    .isNotNull(UserEntity::getBirthday)
                    .isNotNull(UserEntity::getAge)
                    .isNotNull(UserEntity::getMarriage)
                    .isNotNull(UserEntity::getEducation)
                    .isNotNull(UserEntity::getJob)
                    .orderByDesc(UserEntity::getCreateTime);
            // 数据处理
            return dataProcessing(indexOperatorRequest,queryWrapper);
        }
        return new PageObject(
                new ArrayList<>(),
                0,
                0,
                0
        );
    }

    /**
     * 数据处理
     * @param indexOperatorRequest 查询条件
     * @param queryWrapper 条件构造器
     * @return 主页信息
     */
    public PageObject dataProcessing(IndexOperatorRequest indexOperatorRequest, QueryWrapper<UserEntity> queryWrapper){
        Page<UserEntity> UserEntityList = this.page(
                new Page<>(indexOperatorRequest.getPageNum(), indexOperatorRequest.getPageSize()), queryWrapper
        );
        List<IndexResponse> indexResponseList = new ArrayList<>();
        if (UserEntityList.getTotal() != 0) {
            UserEntityList.getRecords().forEach(
                    UserEntity ->{
                        IndexResponse indexResponse = ObjectMapperUtil.convert(UserEntity, IndexResponse.class);
                        if (!StringUtil.isEmpty(UserEntity.getGender())) {
                            indexResponse.setGenderText(dictItemService.getItemName(Constant.DICT_GENDER, UserEntity.getGender()));
                        }
                        if (!StringUtil.isEmpty(UserEntity.getMarriage())) {
                            indexResponse.setMarriageText(dictItemService.getItemName(Constant.DICT_MARRIAGE, UserEntity.getMarriage()));
                        }
                        if (!StringUtil.isEmpty(UserEntity.getEducation())) {
                            indexResponse.setEducationText(dictItemService.getItemName(Constant.DICT_EDUCATION, UserEntity.getEducation()));
                        }
                        if (!StringUtil.isEmpty(UserEntity.getSalary())) {
                            indexResponse.setSalaryText(dictItemService.getItemName(Constant.DICT_SALARY, UserEntity.getSalary()));
                        }
                        indexResponseList.add(indexResponse);
                    }
            );
        }
        return new PageObject(
                indexResponseList,
                (int) UserEntityList.getTotal(),
                (int) UserEntityList.getSize(),
                (int) UserEntityList.getCurrent()
        );
    }
}
